package org.dandelion.moscode;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import org.dandelion.R;

public class MosCodeActivity extends AppCompatActivity {
    private final String[] alpha = {
            "a", "b", "c",
            "d", "e", "f",
            "g", "h", "i",
            "j", "k", "l",
            "m", "n", "o",
            "p", "q", "r",
            "s", "t", "u",
            "v", "w", "x",
            "y", "z"
    };
    private final String[] code = {
            ".－", "－...", "－.－.",
            "－..", ".", "..－.",
            "－－.", "....", "..",
            ".－－－", "－.－", ".－..",
            "－－", "－.", "－－－",
            ".－－.", "－－.－", ".－.",
            "...", "－", "..－",
            "...－", ".－－", "－..－",
            "－.－－", "－－.."
    };

    private Button btn1;
    private Button btn2;
    private Button btnMedia;
    private EditText text1;
    private EditText text2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mos_code);

        btn1 = findViewById(R.id.mingToMi);
        btn2 = findViewById(R.id.miToMing);
        btnMedia = findViewById(R.id.buttonMedia);
        text1 = findViewById(R.id.text1);
        text2 = findViewById(R.id.text2);

        btn1.setOnClickListener(v -> {
            String a = text1.getText().toString();
            String b = jiami(a);
            text2.setText(b);
        });

        btn2.setOnClickListener(v -> {
            String a = text1.getText().toString();
            text2.setText(jiemi(a));
        });

        btnMedia.setOnClickListener(v -> {
            String extract = text2.getText().toString();
            for (int i = 0; i < extract.length(); i++) {
                if (extract.charAt(i) == '.') {
                    MediaPlayer di = MediaPlayer.create(this, R.raw.di);
                    Log.d("sw", "di");
                    di.start();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (di.isPlaying()) {
                        di.stop();
                    }
                    di.release();
                } else if (extract.charAt(i) == '－') {
                    MediaPlayer da = MediaPlayer.create(this, R.raw.da);
                    da.start();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (da.isPlaying()) {
                        da.stop();
                    }
                    da.release();
                    Log.d("sw", "da");
                }
            }
        });
    }

    public String jiami(String s) {
        StringBuilder c = new StringBuilder();
        s = s.toLowerCase();

        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j < alpha.length; j++) {
                if ((String.valueOf(s.charAt(i))).equals(alpha[j])) {
                    c.append(code[j]).append("   ");
                }
            }
        }
        return c.toString();
    }

    public String jiemi(String s) {
        StringBuilder c = new StringBuilder();
        String[] src = s.split(" ");

        for (String value : src) {
            for (int j = 0; j < code.length; j++) {
                if (value.equals(code[j])) {
                    c.append(alpha[j]);
                }
            }
        }
        return c.toString();
    }
}
